package com.mmy.controller.hotel;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.mmy.controller.Common;
import com.mmy.controller.UtilConstants;
import com.mmy.domain.hotel.THotelOrder;
import com.mmy.domain.hotel.THotelPlan;
import com.mmy.domain.hotel.THotelRoom;
import com.mmy.domain.hotel.VHotelPlan;
import com.mmy.domain.sys.TSysUser;
import com.mmy.service.impl.hotel.THotelBaseServiceImpl;
import com.mmy.service.impl.hotel.THotelOrderServiceImpl;
import com.mmy.service.impl.hotel.THotelPlanServiceImpl;
import com.mmy.service.impl.hotel.THotelRoomServiceImpl;
import com.mmy.service.impl.hotel.VHotelBaseServiceImpl;
import com.mmy.service.impl.hotel.VHotelPlanServiceImpl;

/**
 * 类的介绍：酒店订单
 *
 * @author shangzl
 * @date 创建日期：
 * @version 1.0
 */
@Controller
public class THotelOrderController {

	@Autowired
	THotelOrderServiceImpl tHotelOrderServiceImpl;
	@Autowired
	THotelBaseServiceImpl tHotelBaseServiceImpl;
	@Autowired
	VHotelBaseServiceImpl vHotelBaseServiceImpl;
	@Autowired
	THotelPlanServiceImpl tHotelPlanServiceImpl;
	@Autowired
	VHotelPlanServiceImpl vHotelPlanServiceImpl;
	@Autowired
	THotelRoomServiceImpl tHotelRoomServiceImpl;
	@Autowired
	Common common;

	/**
	 * 函数功能说明： 新建酒店预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterreservehotelold")
	public String EnterReserveHotelold(HttpServletRequest request, Model model) {

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));

		List<VHotelPlan> lstHotelPlan = vHotelPlanServiceImpl.findByRoomID(iRoomID);
		model.addAttribute("hotelplanlist", lstHotelPlan);

		model.addAttribute("iRoomID", iRoomID);

		return "webbackground/hotelmanagement/reservehotel";

	}

	@RequestMapping(value = "/subreservehotel")
	public String ReserveHotel(THotelOrder tHotelOrder, HttpSession session, String strPlanID, String RoomID)
			throws Exception {
		// 获取最大ID
		tHotelOrder.setID(common.createMaxID(UtilConstants.TableName.T_HOTEL_ORDER));

		// 获得订单编号
		int iPlanID = Integer.parseInt(strPlanID);
		String cOrderID = common.createOrderCode(UtilConstants.TableName.T_HOTEL_ORDER, iPlanID);

		try {
			THotelPlan tHotelPlan = tHotelPlanServiceImpl.findByPlanID(iPlanID);
			tHotelOrder.setCreateDate(new Date());
			tHotelOrder.setReserveDate(new Date());
			tHotelOrder.setFloor(tHotelPlan.getFloor());
			tHotelOrder.setPeer(tHotelPlan.getPeer());
			tHotelOrder.setPrice(tHotelPlan.getPrice());
			BigDecimal bPrice = tHotelPlan.getPrice();
			int iNumber = tHotelOrder.getOrderNumber();
			BigDecimal bNumber = new BigDecimal(iNumber);
			tHotelOrder.setOrderAmount(bPrice.multiply(bNumber));
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			tHotelOrder.setCreator(tSysUser.getUserName());
			tHotelOrder.setReserveUserID(tSysUser.getUserID());
			tHotelOrder.setOrderID(cOrderID);
			tHotelOrderServiceImpl.save(tHotelOrder.getID());
			tHotelOrder.setOrderState((byte) 0);
			tHotelOrder.setCashierState((byte) 0);
			tHotelOrder.setAccountState((byte) 0);
			tHotelOrder.setInvoiceState((byte) 0);
			tHotelOrder.setRefundState((byte) 0);
			tHotelOrder.setRemittanceState((byte) 0);
			tHotelOrderServiceImpl.save(tHotelOrder);
			return "redirect:hotelorder?iRoomID=" + Integer.parseInt(RoomID);

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 预留转订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterpayorderhotel")
	public String EnterPayOrderHotel(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(iID);
		model.addAttribute("tHotelOrder", tHotelOrder);

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		model.addAttribute("iRoomID", iRoomID);

		return "webbackground/hotelmanagement/payorderhotel";

	}

	@RequestMapping(value = "/subpayorderhotel")
	public String PayOrderHotel(THotelOrder tHotelOrder, HttpSession session, String InTime, String OutTime)
			throws Exception {

		try {
			// 定义一个新的实体类对象
			THotelOrder dtHotelOrder = tHotelOrderServiceImpl.findByID(tHotelOrder.getID());

			dtHotelOrder.setOrderInDate(common.formatDate(InTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
			dtHotelOrder.setOrderOutDate(common.formatDate(OutTime, UtilConstants.DateStyle.YYYY_MM_DD_HH_MM));
			dtHotelOrder.setPayType(tHotelOrder.getPayType());
			dtHotelOrder.setOrderState((byte) 2);
			dtHotelOrder.setOrderDate(new Date());
			dtHotelOrder.setOrderName(tHotelOrder.getOrderName());
			dtHotelOrder.setOrderTel(tHotelOrder.getOrderTel());
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtHotelOrder.setOrderUserID(tSysUser.getUserID());
			dtHotelOrder.setReceivable(tHotelOrder.getReceivable());
			dtHotelOrder.setVoucher(tHotelOrder.getVoucher());
			dtHotelOrder.setReceipt(tHotelOrder.getReceipt());
			tHotelOrderServiceImpl.save(dtHotelOrder);

			return "redirect:printhotel?iID=" + tHotelOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 取消订单
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelorderhotel")
	public String EnterCancelOrderHotel(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));
		THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(iID);
		model.addAttribute("tHotelOrder", tHotelOrder);

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		model.addAttribute("iRoomID", iRoomID);

		return "webbackground/hotelmanagement/cancelorderhotel";

	}

	@RequestMapping(value = "/subcancelorderhotel")
	public String CancelOrderHotel(THotelOrder tHotelOrder, HttpSession session) throws Exception {

		try {
//			 定义一个新的实体类对象 
			THotelOrder dtHotelOrder = tHotelOrderServiceImpl.findByID(tHotelOrder.getID());

			dtHotelOrder.setOrderState((byte) 4);
			dtHotelOrder.setRefund(tHotelOrder.getRefund());
			tHotelOrderServiceImpl.save(dtHotelOrder);

			return "redirect:printcancelhotel?iID=" + tHotelOrder.getID();

		} catch (Exception e) {

		}
		return null;

	}

	/**
	 * 函数功能说明： 修改酒店预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdatereservehotel")
	public String EnterUpdateReserveHotel(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(iID);
		model.addAttribute("tHotelOrder", tHotelOrder);

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		model.addAttribute("iRoomID", iRoomID);

		return "webbackground/hotelmanagement/updatereservehotel";

	}

	@RequestMapping(value = "/updatereservehotel")
	public String UpdateReserveHotel(THotelOrder tHotelOrder, HttpSession session, String RoomID) throws Exception {
		try {
//			 定义一个新的实体类对象 
			THotelOrder dtHotelOrder = tHotelOrderServiceImpl.findByID(tHotelOrder.getID());
			dtHotelOrder.setReserveName(tHotelOrder.getReserveName());
			dtHotelOrder.setReserveTel(tHotelOrder.getReserveTel());
			dtHotelOrder.setOrderNumber(tHotelOrder.getOrderNumber());
			dtHotelOrder.setMemo(tHotelOrder.getMemo());
			dtHotelOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtHotelOrder.getPrice();
			int iNumber = tHotelOrder.getOrderNumber();
			BigDecimal bNumber = new BigDecimal(iNumber);
			dtHotelOrder.setOrderAmount(bPrice.multiply(bNumber));
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtHotelOrder.setReviser(tSysUser.getUserName());
			tHotelOrderServiceImpl.save(dtHotelOrder);
			return "redirect:hotelorder?iRoomID=" + Integer.parseInt(RoomID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 取消预留
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/entercancelreservehotel")
	public String CancelReserveHotel(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(iID);

		tHotelOrder.setOrderState((byte) 3);

		tHotelOrderServiceImpl.save(tHotelOrder);

		model.addAttribute("tHotelOrder", tHotelOrder);
		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		model.addAttribute("iRoomID", iRoomID);

		THotelRoom tHotelRoom = tHotelRoomServiceImpl.findByiRoomID(iRoomID);
		model.addAttribute("tHotelRoom", tHotelRoom);

		List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByRoomID(iRoomID);
		model.addAttribute("orderlist", lstHotelOrder);

		return "webbackground/hotelmanagement/hotelorder";

	}

	/**
	 * 函数功能说明： 修改酒店订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/enterupdateorderhotel")
	public String EnterUpdateOrderHotel(HttpServletRequest request, Model model) {

		int iID = Integer.parseInt(request.getParameter("iID"));

		THotelOrder tHotelOrder = tHotelOrderServiceImpl.findByID(iID);
		model.addAttribute("tHotelOrder", tHotelOrder);
		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		model.addAttribute("iRoomID", iRoomID);

		return "webbackground/hotelmanagement/updateorderhotel";

	}

	@RequestMapping(value = "/updateorderhotel")
	public String UpdateOrderHotel(THotelOrder tHotelOrder, HttpSession session, String RoomID) throws Exception {
		try {
//			 定义一个新的实体类对象 
			THotelOrder dtHotelOrder = tHotelOrderServiceImpl.findByID(tHotelOrder.getID());
			dtHotelOrder.setOrderName(tHotelOrder.getOrderName());
			dtHotelOrder.setOrderTel(tHotelOrder.getOrderTel());
			dtHotelOrder.setOrderNumber(tHotelOrder.getOrderNumber());
			dtHotelOrder.setMemo(tHotelOrder.getMemo());
			dtHotelOrder.setModifyDate(new Date());

			BigDecimal bPrice = dtHotelOrder.getPrice();
			int iNumber = tHotelOrder.getOrderNumber();
			BigDecimal bNumber = new BigDecimal(iNumber);
			dtHotelOrder.setOrderAmount(bPrice.multiply(bNumber));
			TSysUser tSysUser = (TSysUser) session.getAttribute("tSysUser");
			dtHotelOrder.setReviser(tSysUser.getUserName());
			tHotelOrderServiceImpl.save(dtHotelOrder);
			return "redirect:hotelorder?iRoomID=" + Integer.parseInt(RoomID);

		} catch (Exception e) {

		}
		return null;
	}

	/**
	 * 函数功能说明： 删除取消预留信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delreservehotel")
	public String DelReserveHotel(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		tHotelOrderServiceImpl.delHotelOrder(lstID);

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		model.addAttribute("iRoomID", iRoomID);

		THotelRoom tHotelRoom = tHotelRoomServiceImpl.findByiRoomID(iRoomID);
		model.addAttribute("tHotelRoom", tHotelRoom);

		List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByRoomID(iRoomID);
		model.addAttribute("orderlist", lstHotelOrder);

		return "webbackground/hotelmanagement/hotelorder";
	}

	/**
	 * 函数功能说明： 删除取消订单信息
	 *
	 * @param 参数说明
	 * @return 返回值说明 jsp页面
	 * @throws 异常处理说明
	 * @author 创建人 shangzl
	 * @date 创建日期
	 */

	@RequestMapping(value = "/delordershotel")
	public String DelOrderHotel(String checkList, HttpServletRequest request, Model model) {

		String[] checklist = checkList.split(",");
		List<Integer> lstID = new ArrayList<>();

		for (String str : checklist) {
			lstID.add(Integer.parseInt(str));
		}

		tHotelOrderServiceImpl.delHotelOrder(lstID);

		int iRoomID = Integer.parseInt(request.getParameter("iRoomID"));
		model.addAttribute("iRoomID", iRoomID);

		THotelRoom tHotelRoom = tHotelRoomServiceImpl.findByiRoomID(iRoomID);
		model.addAttribute("tHotelRoom", tHotelRoom);
		List<THotelOrder> lstHotelOrder = tHotelOrderServiceImpl.findByRoomID(iRoomID);
		model.addAttribute("orderlist", lstHotelOrder);

		return "webbackground/hotelmanagement/hotelorder";
	}

}
